package com.springinaction.ch03.PlaceholderSpel;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Properties;

@Configuration
/*文件路径  注入到envement*/
//@PropertySource("com/springinaction/ch03/PlaceholderSpel/app.properties")
@ComponentScan("com.springinaction.ch03")
public class PropertyConfig {
    // @Autowired
    //  private Environment env;

    /**
     *  不知道为什么会调用无参的构造函数
     */
//    @Bean
//    public PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() throws IOException {
//        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer =
//                new PropertySourcesPlaceholderConfigurer();
//        propertySourcesPlaceholderConfigurer.setLocation(new ClassPathResource("com/springinaction/ch03/PlaceholderSpel/app.properties"));
//        return propertySourcesPlaceholderConfigurer;
//    }
//
//

/*
      @Bean
      static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws IOException {
            PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
          Properties properties = new Properties();
          File file = new File("src/com/springinaction/ch03/PlaceholderSpel/app.properties");
          System.err.println(file.getAbsolutePath());
          properties.load(new FileInputStream(file));
          propertyPlaceholderConfigurer.setProperties(properties);
          return propertyPlaceholderConfigurer;
      }
*/
      @Bean
      public PropertiesFactoryBean propertiesFactoryBean() {
          PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
          propertiesFactoryBean.setLocation(new ClassPathResource("com/springinaction/ch03/PlaceholderSpel/app.properties"));
          return propertiesFactoryBean;
      }
/*
      @Bean
      public BlankDisc blankDisc(){
          return new BlankDisc(env.getProperty("title"),env.getProperty("artist"));
      }
*/
}
